//-------------------------------------------------------------------
//	Copyright (c) 2010-2014  Zhirnov Andrey
//	This file is part of the "UX Standard Lib" project.
//	See copyright notice in "UX_STL_Core.h".
//-------------------------------------------------------------------

#ifndef _UXS_DIMENSION_PERCENTAGE_H
#define _UXS_DIMENSION_PERCENTAGE_H

#pragma once

#include "../BaseTypes/Types.h"
#include "../CompileTime/TemplateMath.h"
#include "../Math/MathTypes.h"

namespace UX_STL
{
namespace UXTypes
{


	template <typename T>
	struct TPercentage : TypeDescriptor::MustBeFloat<T>
	{
	// types
	public:
		typedef TPercentage<T>		Self;
		typedef T					value_t;


	// variables
	private:
		T	_value;


	// methods
	public:
		TPercentage () : _value(T(0)) {}

		explicit
		TPercentage (T value) : _value(value)	{ ASSERT( value >= T(0) ); }

		T const&	ref ()				const	{ return _value; }
		T &			ref ()						{ return _value; }

		_UX_DIM_ALL_FLOAT_OPERATORS_SELF( _value )

		T			GetPercents ()		const	{ return _value; }					// 0..100%
		T			GetFraction ()		const	{ return _value * T(0.01); }		// 0..1

		template <typename B>
		B			Of (const B &value)	const	{ return value * GetFraction(); }

		string		ToString ()			const	{ return string().FormatF( GetPercents(), StringFormatF().Fmt(0,2) ) << '%'; }

		static Self	From (T value)				{ return FromPercents( value ); }
		static Self	FromPercents (T value)		{ return Self( value ); }
		static Self	FromFraction (T value)		{ return Self( value * T(100.0) ); }

		static Self	Max ()						{ return Self( T(100.0) ); }
		static Self	Min ()						{ return Self( T(0.0) ); }
	};

	typedef TPercentage< UXMath::real >		Percentage;
	

}	// UXTypes
}	// UX_STL

#endif	// _UXS_DIMENSION_PERCENTAGE_H
